package com.weiyi.httpsession;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@WebServlet("/Questionnaire")
public class Questionnaire extends HttpServlet{
	protected void processResquest(HttpServletRequest req,HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter out = resp.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01"+
				" Transitional//EN'>");
		out.println("<html>");
		out.println("<head>");
		out.println("<title>Questionnaire</title>");
		out.println("</head>");
		out.println("<body>");
		String page = req.getParameter("page");
		out.println("<form method='post' action='questionnaire'>");
		if(page == null) {
			out.println("问题一：<input type='text' name='p1q1' /></br>");
			out.println("问题二：<input type='text' name='p1q2' /></br>");
			out.println("<input type='submit' name='page' value='下一页' /></br>");
			
		}else if("下一页".equals(page)) {
			String p1q1 = req.getParameter("p1q1");
			String p1q2 = req.getParameter("p1q2");
			HttpSession session = req.getSession();
			session.setAttribute("p1q1", p1q1);
			session.setAttribute("p1q2", p1q2);
			out.println("问题三：<input type='text' name='p2q1' /></br>");
			out.println("<input type='submit' name='page' value='完成' /></br>");			
		}else {
			HttpSession session = req.getSession();
			out.println(session.getAttribute("p1q1")+"</br>");
			out.println(session.getAttribute("p1q2")+"</br>");
			out.println(req.getParameter("p2q1")+"</br>");
		}
		out.println("</form>");
		out.println("</body>");
		out.println("</html>");
		out.close();
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		processResquest(req,resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		processResquest(req,resp);
	}
	
}
